//  -----------------------------------------------------
//  Project RenderIt
//  Contact : hzzhoukeli@corp.netease.com
//  Author : zhoukeli
//  -----------------------------------------------------
/// @file
/// @path
/// @data 2016/05/04
/// @brief

#pragma once

#include "vector3.hpp"

namespace renderit
{
	namespace math
	{

		class bounding_volume
		{
		public:
			vector3 center;
			vector3 extend;
			float radius;
		};

		class aabb
		{
		public:
			aabb() : inited_(false) {}

		public:
			void union_box(const aabb & b)
			{
				if (!inited_) {
					min_pos = b.min_pos;
					max_pos = b.max_pos;
					inited_ = true;
					return;
				}
				min_pos = vector3(kmin(min_pos.x, b.min_pos.x), kmin(min_pos.y, b.min_pos.y), kmin(min_pos.z, b.min_pos.z));
				max_pos = vector3(kmax(max_pos.x, b.max_pos.x), kmax(max_pos.y, b.max_pos.y), kmax(max_pos.z, b.max_pos.z));
			}

			void union_point(const vector3 & p)
			{
				if (!inited_) {
					min_pos = p;
					max_pos = p;
					inited_ = true;
					return;
				}
				min_pos = vector3(kmin(min_pos.x, p.x), kmin(min_pos.y, p.y), kmin(min_pos.z, p.z));
				max_pos = vector3(kmax(max_pos.x, p.x), kmax(max_pos.y, p.y), kmax(max_pos.z, p.z));
			}

			vector3 get_center() const
			{
				return (min_pos + max_pos) * 0.5;
			}

			vector3 get_size() const
			{
				return (max_pos - min_pos);
			}

			vector3 get_extend() const
			{
				return get_size() * 0.5;
			}

			float get_radius() const
			{
				vector3 extend = get_extend();
				return kmax(extend.x, kmax(extend.y, extend.z));
			}

		public:
			vector3 min_pos;
			vector3 max_pos;
		private:
			bool inited_;
		};
	}
}